package org.dromara.wms.service;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.WmsStockLog;
import org.dromara.wms.mapper.WmsStockLogMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 出入库记录台账Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-05-03
 */
@RequiredArgsConstructor
@Service
public class WmsStockLogService extends BaseService {

    private final WmsStockLogMapper baseMapper;

    /**
     * 查询出入库记录台账
     */
    public WmsStockLog queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询出入库记录台账列表
     */
    public TableDataInfo<WmsStockLog> queryPageList(WmsStockLog bo, PageQuery pageQuery) {
      Page<WmsStockLog> result = baseMapper.queryPageList(pageQuery.build(), bo);
      return TableDataInfo.build(result);
    }

    /**
     * 查询出入库记录台账列表
     */
    public List<WmsStockLog> queryList(WmsStockLog bo) {
        LambdaQueryWrapper<WmsStockLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<WmsStockLog> buildQueryWrapper(WmsStockLog bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WmsStockLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, WmsStockLog::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchCode()), WmsStockLog::getBatchCode, bo.getBatchCode());
        lqw.eq(bo.getPrice() != null, WmsStockLog::getPrice, bo.getPrice());
        lqw.eq(bo.getWarehouseId() != null, WmsStockLog::getWarehouseId, bo.getWarehouseId());
        return lqw;
    }

    /**
     * 新增出入库记录台账
     */
    public Boolean insertByBo(WmsStockLog bo) {
        validEntityBeforeSave(bo);
        return baseMapper.insert(bo) > 0;
    }

    /**
     * 修改出入库记录台账
     */
    public Boolean updateByBo(WmsStockLog bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WmsStockLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出入库记录台账
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
